package com.hanhai.zrb.api.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.hanhai.zrb.api.biz.service.ResourceMatchService;
import com.hanhai.zrb.api.biz.service.ResourceMatchServiceSolr;
import com.hanhai.zrb.model.resourcematch.ResourceMatchContent;
import com.hanhai.zrb.model.resourcematch.ResourceMatchLevelEnum;
import com.hanhai.zrb.model.resourcematch.ResourceMatchResult;
import com.hanhai.zrb.model.resourcematch.ResourceMatchTypeEnum;

@Service
public class ResourceMatchServiceImpl implements ResourceMatchService {
	private Logger logger = Logger.getLogger(getClass());

	@Resource
	private ResourceMatchServiceSolr resourceMatchServiceSolr;

	/**
	 * 推荐匹配资源，优先从redis缓存中取
	 * 
	 * @param type
	 *            项目意向，资金意向,项目，资金
	 * @param bizid
	 *            业务id，意向、资金、项目
	 * @return 最多前3位的匹配资源(项目/资金)
	 */
	@Override
	public ResourceMatchResult recommend(Integer type, Long bizid) {
		List<ResourceMatchContent> list = null;
		ResourceMatchResult match = resourceMatchServiceSolr.match(type,
				bizid, true,ResourceMatchLevelEnum.ALL);
		List<ResourceMatchContent> matchList = match.getResourceMatchContentList();
		if (matchList != null && matchList.size() > 3) {
			list = matchList.subList(0, 3);
		} else {
			list = matchList;
		}
		ResourceMatchResult result = new ResourceMatchResult();
		result.setResourceMatchContentList(list);
		result.setResourceMatchResource(match.getResourceMatchResource());
		return result;
	}

	/**
	 * 所有匹配资源，实时匹配，不从redis缓存中取
	 * 
	 * @param type
	 *            @see {@link ResourceMatchTypeEnum} 项目意向，资金意向,项目，资金
	 * @param bizid
	 *            业务id，意向、资金、项目
	 * @return 最多前3位的匹配资源(项目/资金)
	 */
	@Override
	public ResourceMatchResult matchPage(Integer type, Long bizid,
			Integer pageNo, Integer pageSize,ResourceMatchLevelEnum level) {
		
		ResourceMatchResult match = resourceMatchServiceSolr.match(type,
						bizid, false,level);
		List<ResourceMatchContent> matchList = match.getResourceMatchContentList();
		if (pageNo == null || pageNo < 0) {
			pageNo = 1;
		}
		if (pageSize == null || pageSize < 1 || pageSize > 50) {
			pageSize = 10;
		}
		int size = 0;
		if (matchList != null) {
			size = matchList.size();
		}

		Integer pageCount = 0;
		if (size > 0) {
			if (size % pageSize == 0) {
				pageCount = size / pageSize;
			} else {
				pageCount = size / pageSize + 1;
			}
			if (pageNo > pageCount) {
				pageNo = 1;
				logger.error("The pageNo is more than pageCount~set pageNo=1");
			}
		}
		Page<ResourceMatchContent> page = new Page<ResourceMatchContent>(pageNo, pageSize, size);
		List<ResourceMatchContent> list = null;
		if (matchList != null && size > 0) {
			Integer start = page.getStartRow();
			Integer end = page.getEndRow();
			if (start < 0 || start > size) {
				start = 0;
			}
			if (end < 0 | end > size) {
				end = size;
			}
			list = matchList.subList(start, end);
			page.addAll(list);
		}
		ResourceMatchResult result = new ResourceMatchResult();
		result.setResourceMatchContentPage(page);
		result.setResourceMatchResource(match.getResourceMatchResource());
		return result;
	}
}
